% --------------- Plot spectrum with data of MRS experiments ------------- 
% Created Date    : 05 Mar 2013
% Modified Date   ; 05 Mar 2013
% Author Name     : Sungjin Kim
% Arguments
%     (Data,
%      transmitter frequency,
%      spectrum bandwidth,
%      selection of frequency domain or time domain,
%      selection of real or imaginary,  
%      selection of display-window,
%      handles)
% ------------------------------------------------------------------------ 

%% BEGIN OF FUNCTION %%
function plotspectra(data, frequencyTx, bandwidth, sorts, Real_Imaginary_sel, window_sel, handles) 
    
    % make x-scale, using the number of data points %
    len_data = whos('data');
    len_data = len_data.size(1);
             
    % selection of whether frequency or time domain axis %
    if(sorts == 1)
        % make label array %
        data = flipud(data);
        x_axis = -len_data/(2*frequencyTx) : 1/frequencyTx : (len_data-1)/(2*frequencyTx); % convert to ppm
        len_label = (abs(int32(x_axis(1)))+1)*2+1;   % bandwidth in ppm
%         label_x = zeros(len_label, 2);
%         label_x = char(label_x);
                    
        % Real spectrum %
        if(Real_Imaginary_sel == 1)
            if window_sel == handles.referenceSpectrum
                plot(window_sel, x_axis, real(data), 'red');
                title('Reference Spectrum - Real', 'fontsize', 13);
                ylim(window_sel, [-200, 200]);
                set(window_sel,'XTick', -len_label : 1 : len_label, 'XDir', 'reverse');
                xlabel(handles.referenceSpectrum, 'ppm');
                ylabel(handles.referenceSpectrum, 'Amplitude');
            elseif window_sel == handles.current_spectrum
                plot(window_sel, x_axis, real(data), 'black');
                title('Adjusting Spectrum - Real', 'fontsize', 13);
                ylim([-200, 200]);
                set(window_sel,'XTick', -len_label : 1 : len_label, 'XDir', 'reverse');
                xlabel(handles.current_spectrum,'ppm');
                ylabel(handles.current_spectrum,'Amplitude');
            elseif window_sel == 3
                figure('name', 'Difference of Spectra', 'Position', [200 100 1470 300],'color', [1 1 1]);
                view1 = axes;            
                plot(view1, x_axis, real(data), 'm');
                title('Difference Spectrum - Real', 'fontsize', 13);
                ylim([-200, 200]);
                set(view1 ,'XTick', -len_label : 1 : len_label, 'XDir', 'reverse');
                xlabel(view1, 'ppm');
                ylabel(view1, 'Amplitude');
            else
                fprintf('Wrong window selection\n');
            end
            
        elseif(Real_Imaginary_sel == 2)
            % Imaginary spectrum %
           if window_sel == handles.referenceSpectrum
                plot(window_sel, x_axis, imag(data));
                title('Reference Spectrum - Imaginary', 'fontsize', 13);
%                 ylim([-12000, 12000]);
                set(window_sel,'XTick', -len_label : 1 : len_label, 'XDir', 'reverse');
                xlabel('ppm');
                ylabel('Amplitude');
            else
                plot(window_sel, x_axis, imag(data));
                title('Adjusting Spectrum - Imaginary', 'fontsize', 13);
%                 ylim([-12000, 12000]);
                set(window_sel,'XTick', -len_label : 1 : len_label, 'XDir', 'reverse');
                xlabel('ppm');
                ylabel('Amplitude');
            end
        else
            sprintf('incorrect real or imaginary selection!! \n');
        end
        
    elseif(sorts == 2)
        % make label array %
        x_axis = 0 : 1/bandwidth : (len_data-1)/bandwidth;   % convert to time
        x_axis = x_axis * 1000;
        len_label = (abs(int32(x_axis(1)))+1)*2+1;
        label_x = zeros(len_label, 2);
        label_x = char(label_x);

        % Real FID %
        plot(handles.realspectra, x_axis, real(data), 'red');
        ylim([-60, 60]);
        title('Real', 'fontsize', 12);
        set(gca,'XTick', 0 : 100 : x_axis(len_data));
        xlabel('ms');
        ylabel('Amplitude');

        % Imaginary FID %
        plot(handles.imagspectra, x_axis, imag(data));
        ylim([-60, 60]);
        title('Imaginary',  'fontsize', 12);
        set(gca,'XTick', 0 : 100 : x_axis(len_data));
        xlabel('ms');
        ylabel('Amplitude');        
    else
        disp('You select a wrong sorts of axis. (1 or 2)');
        return;
    end
    
    return;
    
end % END OF FUNCTION %